package com.rain.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.rain.activity.adapter.QuestionAdapter;
import com.rain.activity.utils.BaseActivity;
import com.rain.activity.utils.QuestionDAO;

import java.util.List;
import java.util.Map;

public class QuestionActivity extends BaseActivity {
    private ListView list;
    private List<Map<String,Object>> values;
    private QuestionAdapter adapter;
    //分页显示
    private int pageSize=15;
    private int pageNum =1;
    private int recordCount;
    private int pageCount;

    private int first;
    private  int visible;
    private int total;
    private  TextView footView;
    private String keyword;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_question);
        super.init("全部问题", 1);
        list= (ListView) findViewById(R.id.list);
        keyword = getIntent().getStringExtra("keyword");
        if (keyword==null){
            keyword = "";
        }
        values= QuestionDAO.loadDataPage(pageNum, pageSize,keyword);
        recordCount = QuestionDAO.getPageCount(keyword);
        pageCount = (recordCount-1)/pageSize+1;
        footView = new TextView(this);
        footView.setText("正在加载，请稍后。。。");
        footView.setTextColor(Color.BLACK);
        footView.setTextSize(14);
        list.addFooterView(footView);
        adapter = new QuestionAdapter(this,values);
        list.setAdapter(adapter);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                LinearLayout layout = (LinearLayout) view;
                Map<String, Object> map = values.get(position);
                if (layout.getChildCount() == 1) {
                    TextView tv = new TextView(QuestionActivity.this);
                    tv.setText(map.get("ans").toString());
                    tv.setTextSize(14);
                    tv.setTextColor(Color.RED);
                    layout.addView(tv);
                    map.put("showflag",true);
                } else {
                    layout.removeViewAt(1);
                    map.put("showflag",false);
                }
            }
        });
        list.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {
                if(scrollState == SCROLL_STATE_IDLE && first + visible == total&& total != 0){
                    if(pageNum<pageCount){
                        pageNum++;
                        values.addAll(QuestionDAO.loadDataPage(pageNum,pageSize,keyword));
                        adapter.notifyDataSetChanged();
                    }
                    else{
                        if(list.getFooterViewsCount()>0)
                          list.removeFooterView(footView);
                    }
                }
            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                first = firstVisibleItem;
                visible = visibleItemCount;
                total = totalItemCount;
            }
        });
    }
}
